package sol.leetcode;

import java.util.Arrays;

/**
 * Product of Array Except Self
 */
public class Issue238 {
    public int[] productExceptSelf(int[] nums) {
        int len = nums.length;

        if (len<=1)
            return new int[]{0};

        int[] prefix = new int[len + 1];
        int[] suffix = new int[len + 1];
        prefix[0] = 1;
        suffix[len] = 1;

        for (int i = 0; i < len; ++i) {
            prefix[i + 1] = prefix[i] * nums[i];
            int ii = len - i-1;
            suffix[ii] = suffix[ii + 1] * nums[ii];
        }

        for (int i = 0; i < len; ++i)
            nums[i] = prefix[i] * suffix[i + 1];

        return nums;
    }

    public static void main(String[] args) {
        int[] nums = {1,2};
        System.out.println(Arrays.toString(new Issue238().productExceptSelf(nums)));
    }
}
